package com.boot.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.httpclient.util.DateUtil;
import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.http.NameValuePair;
import org.apache.http.entity.ContentType;
import org.apache.http.message.BasicNameValuePair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class PushTaskTest {
    private final static Logger log = LoggerFactory.getLogger(PushTaskTest.class.getName());
    public static void main(String[] args) throws UnsupportedEncodingException {

        String pushtaskTokenUrl="http://localhost/a/event/open/getToken";
        String pushtaskTaskUrl="http://localhost/a/event/open/pushtask";
        String businessid="tuijian";
        String PUSH_SERVER_PRE="open_push_server_";
        String biztype="6";
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("businessid", businessid));
        String result = HttpUtils.httpPost(pushtaskTokenUrl, params, 30000, "UTF-8", null);
        log.info("token result: {}", result);

        JSONObject obj = JSON.parseObject(result);
        String token="";
        try {
            token = Base64Utils.decode(obj.get("message").toString()).replace(PUSH_SERVER_PRE + businessid, "");
        }catch (Exception e){
            e.printStackTrace();
        }
        params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("appid", "100010")); // APP标识
        params.add(new BasicNameValuePair("title", "测试任务")); // 活动标题
        params.add(new BasicNameValuePair("devicetype", "0")); // 全部设备
        params.add(new BasicNameValuePair("type", "3")); // 需要精准推送
        params.add(new BasicNameValuePair("biztype", biztype)); // 推送类型

        // linkurl
        // params.add(new BasicNameValuePair("bizid", "-1")); //
        // params.add(new BasicNameValuePair("contenttype", "0"));
        // params.add(new BasicNameValuePair("linkurl", "autohome://article/platform?newsid=2886407&pageindex=1&type=1&showcommentlist=0"));

        // 文章最终页
        params.add(new BasicNameValuePair("bizid", "-1"));
        params.add(new BasicNameValuePair("contenttype", "30"));
        params.add(new BasicNameValuePair("linkurl", ""));

        params.add(new BasicNameValuePair("content", URLEncoder.encode("长城WEY与领克谁是强中强", "UTF-8")));
        params.add(new BasicNameValuePair("backid", "0x61"));
        params.add(new BasicNameValuePair("expire", "3"));
        params.add(new BasicNameValuePair("opid", "007"));
        params.add(new BasicNameValuePair("opname", "lisong"));
        params.add(new BasicNameValuePair("appconfig", "0"));// 是够过滤用户手机中设置的配置
        params.add(new BasicNameValuePair("joinsta", "0"));
        params.add(new BasicNameValuePair("interval", "1")); // 1小时之内只推一条
        params.add(new BasicNameValuePair("businessid", "0"));
        params.add(new BasicNameValuePair("triggertime", DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss")));
        params.add(new BasicNameValuePair("shortcontent", URLEncoder.encode("汽车之家", "UTF-8")));
        params.add(new BasicNameValuePair("token", Base64Utils.encode(token.getBytes())));

        log.info(JSONObject.toJSONString(params));
        String zip="d:\\0.zip";
        result = HttpUtils.httpClientUploadFile(zip, params, pushtaskTaskUrl, 3000, "UTF-8", null, ContentType.APPLICATION_JSON);
        log.info(result);
    }
}
